﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmTRegistro : Form
    {
        [Dependency]
        public IContratoBL contratoBL { get; set; }
        public FrmTRegistro()
        {
            InitializeComponent();
            this.BuildUp();
        }


        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void ReiniciarFechas()
        {
            dateTimePicker1.Value = DateTime.Now.Date;
            var fec2 = dateTimePicker1.Value.Date;
            dateTimePicker2.Value = fec2.AddDays(1);
        }
        private void FrmTRegistro_Load(object sender, EventArgs e)
        {
            cONTRATOBindingSource.DataSource = contratoBL.GetAllFromContratos();
            ReiniciarFechas();
        }
        void CargaData()
        {
            var fec1 = dateTimePicker1.Value.Date;
            var fec2 = dateTimePicker2.Value.Date;

            if (fec1 <= fec2)
            {
                cONTRATOBindingSource.DataSource = contratoBL.GetFromContratobyRangoFechas(fec1, fec2);
            }
            else
            {
                ReiniciarFechas();
                MessageBox.Show("La fecha de inicio debe ser menor o igual a la fecha de término", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var tilde = Convert.ToString("´");
            string cadena = tilde;


            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

       
    }
}
